package com.dragon.english_practice_back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragon.english_practice_back.entity.Message;
import com.dragon.english_practice_back.reponse.Result;
import com.dragon.english_practice_back.service.AuthenticationUtil;
import com.dragon.english_practice_back.service.MessageService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    AuthenticationUtil authenticationUtil;
    MessageService messageService;


    //获取对话下的所有消息
    @GetMapping("/getAll/{sessionId}")
    public Result<List<Message>> getAllMessages(@PathVariable Long sessionId) {
        // 验证用户是否有权限访问该session
        if (!messageService.hasAccessToSession(authenticationUtil.id(), sessionId)) {
            return new Result<>(403, "没有权限访问此会话", null);
        }

        // 获取session下的所有消息
        List<Message> messages = messageService.list(new QueryWrapper<Message>()
                .select("id", "content", "create_date", "type")
                .orderByAsc("id")
                .eq("session_id", sessionId)
                .eq("creator_id", authenticationUtil.id()));

        return new Result<>(200, "成功获取消息", messages);
    }
}
